package screens
{
	import flash.geom.Point;
	import objects.Star_Effect;
	import starling.display.Image;
	import starling.display.Sprite;
	import events.NavigationEvent;
	import starling.events.EnterFrameEvent;
	import starling.extensions.PDParticleSystem;
	import starling.text.TextField;
	import objects.ManagerEffect;
	import starling.textures.Texture;
	import starling.events.EnterFrameEvent;
	import flash.external.ExternalInterface;
	/**
	 * ...
	 * @author
	 */
	public class Loading extends Sprite
	{
		public var m_loadComplete:Boolean;
		public var m_next:Boolean;
		public var m_background:Image;
		public var m_background1:Image;
		public var m_process:Image;
		public var m_textBackground:TextField;
		public var m_percentProcess:TextField;
		public var m_testLoadSolider:Boolean;
		public var m_testLoadTower:Boolean;
		public var m_testLoadCanon:Boolean;
		public var m_testLoadStage:Boolean;
		public var m_testLoadUser:Boolean;
		public var m_testLoadLink:Boolean;
		public var m_testLoadStarStage1:Boolean;
		public var m_testLoadStarStage2:Boolean;
		public var m_testLoadUpdateStar:Boolean;
		public var m_testLoadHuongTest:Boolean;
		public var m_testReLoad:Boolean;
		public var m_testCreatNew:Boolean;
		
		public function Loading()
		{
			ManagerEffect.instance.m_layer = this;
			this.addEventListener(EnterFrameEvent.ENTER_FRAME, update);
			m_background = new Image(Assets.getTexture("load1"));
			m_background.pivotX = m_background.width / 2;
			m_background.pivotY = m_background.height / 2;
			m_background.x = 1050 / 2;
			m_background.y = 700 / 2;
			
			m_process = new Image(Assets.getTexture("load2"));
			m_process.x = -136;
			m_process.y = 278;
			
			m_background1 = new Image(Assets.getTexture("load3"));
			m_background1.x = 308;
			m_background1.y = 278;
			
			this.addChild(m_background1);
			this.addChild(m_process);
			this.addChild(m_background);
			
			m_textBackground = new TextField(500, 50, "", "MyFontName", 30, 0xFFFFFF);
			m_percentProcess = new TextField(150, 50, "", "MyFontName", 15, 0xFFFFFF);
			this.addChild(m_textBackground);
			this.addChild(m_percentProcess);
			m_textBackground.text = "Tải dữ liệu người dùng";
			m_textBackground.x = 300;
			m_textBackground.y = 600;
			m_percentProcess.x = 500;
			m_percentProcess.y = 300;
			//database.instance.UserName = ExternalInterface.call("Get_User_Name");
			database.instance.UserName = "Vuong";
			//m_textBackground.text = "ten nguoi dung:" + database.instance.UserName;
		}
		public function GetData():void
		{
			database.instance.TestLoad();
			if (database.instance.RESULD != "Khong Load Duoc")
			{
				/////////////////////////////////////////
				if (m_testLoadSolider == false)
				{
					database.instance.getDataSolider();
					m_testLoadSolider = true;
					m_process.x += 441 / 8;
					m_percentProcess.text = "1/8: Dữ liệu Chiến binh";
					ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML3()), Texture.fromBitmap(new AssetsParticles.ParticleTexture5())), new Point(m_process.x+441, m_process.y+50), 50));
				}
				if (database.instance.RESULTSOLIDERARRAY != null &&m_testLoadTower == false)
				{
					database.instance.getDataTower();
					m_testLoadTower = true;
					m_process.x += 441 / 8;
					m_percentProcess.text = "2/8: Dữ liệu Cung binh";
					ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML3()), Texture.fromBitmap(new AssetsParticles.ParticleTexture5())), new Point(m_process.x+441, m_process.y+50), 50));
				}
				if (database.instance.RESULTSOLIDERARRAY != null && database.instance.RESULDTOWERARRAY != null && m_testLoadCanon == false)
				{
					database.instance.getDataCanon();
					m_testLoadCanon = true;
					m_process.x += 441 / 8;
					m_percentProcess.text = "3/8: Dữ liệu Pháo binh";
					ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML3()), Texture.fromBitmap(new AssetsParticles.ParticleTexture5())), new Point(m_process.x+441, m_process.y+50), 50));
				}
				if (database.instance.RESULTSOLIDERARRAY != null && database.instance.RESULDTOWERARRAY != null && database.instance.RESULTCANONARRAY != null && m_testLoadStage == false)
					
				{
					database.instance.getStage();
					m_testLoadStage = true;
					m_process.x += 441 / 8;
					m_percentProcess.text = "4/8: Dữ liệu Trận đánh";
					ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML3()), Texture.fromBitmap(new AssetsParticles.ParticleTexture5())), new Point(m_process.x+441, m_process.y+50), 50));
				}
				if (database.instance.RESULTSOLIDERARRAY != null && database.instance.RESULDTOWERARRAY != null && database.instance.RESULTCANONARRAY != null && m_testLoadStarStage1 == false && database.instance.RESULTSTAGE != null)
				{
					database.instance.getStarStage1();
					m_testLoadStarStage1 = true;
					m_process.x += 441 / 8;
					m_percentProcess.text = "5/8: Dữ liệu Trận 1";
					ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML3()), Texture.fromBitmap(new AssetsParticles.ParticleTexture5())), new Point(m_process.x+441, m_process.y+50), 50));
					
				}
				if (database.instance.RESULTSOLIDERARRAY != null && database.instance.RESULDTOWERARRAY != null && database.instance.RESULTCANONARRAY != null && database.instance.RESULTSTAGE != null && database.instance.RESULTSTARSTAGE1 != null && m_testLoadStarStage2 == false)
				{
					database.instance.getStarStage2();
					m_testLoadStarStage2 = true;
					m_process.x += 441 / 8;
					m_percentProcess.text = "6/8: Dữ liệu Trận 2";
					ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML3()), Texture.fromBitmap(new AssetsParticles.ParticleTexture5())), new Point(m_process.x+441, m_process.y+50), 50));
				}
				if (database.instance.RESULTSOLIDERARRAY != null && database.instance.RESULDTOWERARRAY != null && database.instance.RESULTCANONARRAY != null && database.instance.RESULTSTAGE != null && database.instance.RESULTSTARSTAGE1 != null && database.instance.RESULTSTARSTAGE2 != null && m_testLoadHuongTest == false)
				{
					database.instance.getDataHuongTest();
					m_testLoadHuongTest = true;
					m_process.x += 441 / 8;
					m_percentProcess.text = "7/8: Dữ liệu Trạng nguyên";
					ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML3()), Texture.fromBitmap(new AssetsParticles.ParticleTexture5())), new Point(m_process.x+441, m_process.y+50), 50));
				}
				if (database.instance.RESULTSOLIDERARRAY != null && database.instance.RESULDTOWERARRAY != null && database.instance.RESULTCANONARRAY != null && database.instance.RESULTSTAGE != null && database.instance.RESULTSTARSTAGE1 != null && database.instance.RESULTSTARSTAGE2 != null && database.instance.RESULDARRAYHUONGTEST != null && m_testLoadUpdateStar == false)
				{
					database.instance.getDataLevelUpdateStar();
					m_testLoadUpdateStar = true;
					m_process.x += 441 / 8;
					m_percentProcess.text = "8/8: Dữ liệu Nâng cấp";
					ManagerEffect.instance.AddEff(new Star_Effect(ManagerEffect.instance.m_layer, new PDParticleSystem(XML(new AssetsParticles.ParticleXML3()), Texture.fromBitmap(new AssetsParticles.ParticleTexture5())), new Point(m_process.x+441, m_process.y+50), 50));
				}
				if (database.instance.RESULTSOLIDERARRAY != null && database.instance.RESULDTOWERARRAY != null && database.instance.RESULTCANONARRAY != null && database.instance.RESULTSTAGE != null && database.instance.RESULTSTARSTAGE1 != null && database.instance.RESULTSTARSTAGE2 != null && database.instance.RESULDARRAYHUONGTEST != null && database.instance.RESULTLEVELUPDATESTAR != null)
				{
					m_loadComplete = true;
				}
				if (m_next == true)
				{
					var i:int = database.instance.RESULTSOLIDERARRAY[0];
					this.removeEventListener(EnterFrameEvent.ENTER_FRAME, update);
					this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, { id: "menu" }, true));
				}
			}
			else
			{
				m_percentProcess.text = "Có lỗi trong quá trình tải dữ liệu";
				m_process.x = -136;
			}
			
		}
		private function update(e:EnterFrameEvent):void
		{
			ManagerEffect.instance.update(Global.FRAME);
			if (database.instance.UserName != null && m_testLoadUser == false)
			{
				database.instance.m_testGetUser = false;
				database.instance.getUser();
				m_testLoadUser = true;
			}
			if (database.instance.m_testGetUser == true)
			{
				if (database.instance.m_user != "")
				{
					//m_textBackground.text = "Nguoi duoc tim thay: " + database.instance.m_user;
					GetData();
				}
				else
				{
					if (m_testCreatNew == false)
					{
						database.instance.CreateNew();
						m_testCreatNew = true;
						m_testReLoad = true;
						
					}
					m_testLoadUser = false;
				}
			}
		
		}
		
		public function disposeTemporarily():void
		{
			ManagerEffect.instance.ListEff.splice(0, ManagerEffect.instance.ListEff.length);
			this.visible = false;
		}
	}

}